<?php
namespace Emos\Field;

class TabSet extends ItemSet {    
    var $elementSets = array();
    
    public function addTab($label, ItemSet $elementSet) {
        $this->elementSets[] = array(
            'label'      => $label,
            'elementSet' => $elementSet,
        );
    }
    
    public function getTemplate() {
        ob_start();
        
        $pref = 'id-'.mt_rand(0, mt_getrandmax());
        
        print "<div class='tabs'>";
        print "<ul>";
        foreach($this->elementSets as $name => $elementSet) {            
            print "<li><a href='#{$pref}-{$name}'>{$elementSet['label']}</a></li>";
        }
        print "</ul>";        

        foreach($this->elementSets as $name => $elementSet) {            
            print "<div id='{$pref}-{$name}'>";
            print $elementSet['elementSet']->getTemplate();
            print "</div>";
        }        
        print "</div>";  
        
        return ob_get_clean();
    }
    
    public function getControls() {
        $fields = array();
        foreach($this->elementSets as $name=>$elementSet) {
            $fields += $elementSet['elementSet']->getControls();
        }
        return $fields;
    }    
}